﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace QuanLyThuoc
{
    class DanhSachNhanVien
    {
        int n;
        static List<NhanVien> DSNV;
        
        public DanhSachNhanVien()
        { 
            DSNV = new List<NhanVien>();
        }

        public void ThemNV()
        {
            Console.Write("\nSo nhan vien them vao: ");
            while (int.TryParse(Console.ReadLine(), out n) == false)
            {
                Console.WriteLine("Nhap sai! Vui long nhap lai!");
            }
            for (int i = 0; i < n; i++)
            {
                NhanVien NV = new NhanVien();
                NV.NhapNV();
                DSNV.Add(NV);
                NV.ghiFile();
            }
        }

        public void XuatNV()
        {
            foreach (NhanVien NV in DSNV)
            {
                NV.XuatNV();
            }
        }

        public static NhanVien TimNVTheoMa(string matim)
        {
            foreach (NhanVien NV in DSNV)
            {
                if (matim == NV.MaNV)
                {
                    return NV;
                }
            }
            return null;
        }

        public List<NhanVien> TimNVTheoTen(string tentim)
        {
            List<NhanVien> dsNV = new List<NhanVien>();
            foreach (NhanVien NV in DSNV)
            {
                if (NV.HoTenNV.Contains(tentim))
                    dsNV.Add(NV);
            }
            return dsNV;
        }

        public void TimKiem()
        {
            int x = -1;
            Console.WriteLine("\nChon chuc nang tim: \n\t1: Theo ma \n\t2: Theo ten");
            while (int.TryParse(Console.ReadLine(), out x) == false)
            {
                Console.WriteLine("Nhap sai, hay nhap lai!");
            }
            switch (x)
            {
                case 1:
                    Console.Write("\nNhap ma nhan vien can tim: ");
                    string maNVtim = Console.ReadLine();
                    NhanVien NVtim = TimNVTheoMa(maNVtim);
                    if (NVtim.MaNV != null)
                    {
                        Console.WriteLine("\tMa \tHo ten \tNgay sinh \tDia chi \tDien thoai \tHe so luong");
                        NVtim.XuatNV();
                    }
                    else
                        Console.WriteLine("Khong tim thay");
                    break;
                case 2:
                    Console.Write("\nNhap ten nhan vien can tim: ");
                    string tentim = Console.ReadLine();
                    List<NhanVien> TimTen = TimNVTheoTen(tentim);
                    if (TimTen.Count > 0)
                    {
                        Console.WriteLine("\tMa \tHo ten \tNgay sinh \tDia chi \tDien thoai \tHe so luong");
                        foreach (NhanVien NV in DSNV)
                        {
                            NV.XuatNV();
                        }
                    }
                    else
                        Console.WriteLine("Khong tim thay!");
                    break;
            }
        }

        public bool XoaNV(string maxoa)
        {
            NhanVien VtXoa = TimNVTheoMa(maxoa);
            if (VtXoa != null)
            {
                DSNV.Remove(VtXoa);
                n--;
                return true;
            }
            return false;
        }

        /*public bool SuaNV(string masua, string ten, DateTime ngaysinh, string diachi, string dienthoai, float hesoluong)
        {
            NhanVien VtSua = TimNVTheoMa(masua);
            if (VtSua!=null)
            {
                VtSua.HoTenNV = ten;
                VtSua.NgaySinh = ngaysinh;
                VtSua.DiaChi = diachi;
                VtSua.DienThoai = dienthoai;
                VtSua.HeSoLuong = hesoluong;
                return true;
            }
            return false;
        }*/

        public bool SuaDSNV(string masua)
        {
            NhanVien Sua = TimNVTheoMa(masua);
            if (Sua != null)
            {
                int x = -1;
                while (x != 0)
                {
                    Console.WriteLine("Chon doi tuong can sua: \n\t1: Ho ten \n\t2: Ngay sinh \n\t3: Dia chi \n\t4: Dien thoai \n\t5: He so luong");
                    while (int.TryParse(Console.ReadLine(), out x) == false)
                    {
                        Console.WriteLine("Chuc nang khong ton tai!");
                    }
                    switch (x)
                    {
                        case 1:
                            Console.Write("\nNhap ten can sua: ");
                            Sua.HoTenNV = Console.ReadLine();
                            break;
                        case 2:
                            Console.Write("\nNhap ngay sinh sua: ");
                            Sua.NgaySinh = DateTime.Parse(Console.ReadLine());
                            break;
                        case 3:
                            Console.Write("\nNhap dia chi sua: ");
                            Sua.DiaChi = Console.ReadLine();
                            break;
                        case 4:
                            Console.Write("\nNhap so dien thoai moi: ");
                            Sua.DienThoai = Console.ReadLine();
                            break;
                        case 5:
                            Console.Write("\nNhap he so luong moi: ");
                            Sua.HeSoLuong = float.Parse(Console.ReadLine());
                            break;
                    }
                    return true;
                }
            }
            else
            {
                Console.WriteLine("\nKhong tim thay nhan vien can sua");
                return false;
            }
            return false;
        }

        //doc file
        public void docFile()
        {
            FileStream file = new FileStream("DSNhanVien.txt", FileMode.Open);
            BinaryReader br = new BinaryReader(file);
            while (br.PeekChar() != -1)
            {
                NhanVien NV = new NhanVien();
                NV.MaNV = br.ReadString();
                NV.HoTenNV = br.ReadString();
                NV.NgaySinh = DateTime.Parse(br.ReadString());
                NV.DiaChi = br.ReadString();
                NV.DienThoai = br.ReadString();
                NV.HeSoLuong = float.Parse(br.ReadString());
                DSNV.Add(NV);
            }
            br.Close();
            file.Close();
        }
    }
}
